package service;

import entity.AccessToken;
import org.json.JSONObject;
import util.Request;
import util.XmlClass;

import java.util.Map;

public class AccessTokenTest {
//    static InputStream in= AccessTokenTest.class.getClassLoader().getResourceAsStream("wx-config.xml");
//    static Map<String,String> Xmlmap=wxService.parseRequest(in);
    static Map<String,String>Xmlmap=new XmlClass().getXml();
    private static final String APPID=Xmlmap.get("appidTest");
    private static final String SECRT=Xmlmap.get("secrtTest");
    private static final String GET_TOKEN_URL=Xmlmap.get("getToken");

    private static AccessToken at;

    private static void getToken(){
        String url=GET_TOKEN_URL.replace("APPID",APPID).replace("APPSECRET",SECRT);
        String tokenstr= Request.get(url);
        JSONObject jsonObject=new JSONObject(tokenstr);
        String token=jsonObject.getString("access_token");
        int expires=jsonObject.getInt("expires_in");
        at=new AccessToken(token,expires);
    }
    public static String getAccessToken(){
        if(at==null||at.isExpires()){
            getToken();
        }
        return at.getToken();
    }

}
